package fr.opensagres.xdocreport.examples.docxandfreemarker;

import fr.opensagres.xdocreport.core.XDocReportException;
import fr.opensagres.xdocreport.document.IXDocReport;
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
import fr.opensagres.xdocreport.template.IContext;
import fr.opensagres.xdocreport.template.TemplateEngineKind;
import freemarker.ext.dom.NodeModel;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.*;

/**
 * @author yzc
 * @description https://github.com/opensagres/xdocreport/wiki/Reporting
 * https://github.com/opensagres/xdocreport/wiki/DocxReportingJavaMain
 * XDocReport  支持缓存，可以提高转换的性能。
 * @date 2018/9/6
 */
public class DocxXMLWithFreemarker {

    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
        try {
            String fileIn = "/freemarker/template.docx";
            String fileOut = "/Users/yuanzhenchao/tmp/test3.docx";
            String fileOut2 = "/Users/yuanzhenchao/tmp/test4.docx";

            // 1) Load Docx file by filling Freemarker template engine and cache
            // it to the registry
            String reportId = "report-id-1";
            InputStream in = DocxXMLWithFreemarker.class.getResourceAsStream(fileIn);
            XDocReportRegistry registry = XDocReportRegistry.getRegistry();
            IXDocReport report = registry.loadReport(in, reportId, TemplateEngineKind.Freemarker, true);

            // 2) Create fields metadata to manage lazy loop (#forech velocity)
            // for table row.
            /*FieldsMetadata metadata = report.createFieldsMetadata();
            metadata.addFieldAsList("doc.project.developer.@name");
            metadata.addFieldAsList("developers.lastName");
            metadata.addFieldAsList("developers.mail");*/

            // 3) Create context Java model
            IContext context = report.createContext();
            InputStream xmlIn = DocxXMLWithFreemarker.class.getResourceAsStream("/freemarker/template.xml");
            InputSource inputSource = new InputSource(xmlIn);
            NodeModel model = NodeModel.parse(inputSource);
            context.put("attr", model);

            // 4) Generate report by merging Java model with the Docx
            OutputStream out = new FileOutputStream(new File(fileOut));
            report.process(context, out);
            System.out.println( "Process docx with " + ( System.currentTimeMillis() - startTime ) + " ms." );

            // generate report by get registry
            startTime = System.currentTimeMillis();
            IXDocReport cachedReport = registry.getReport(reportId);
            OutputStream out2 = new FileOutputStream(new File(fileOut2));
            cachedReport.process(context, out2);
            System.out.println( "Process docx with " + ( System.currentTimeMillis() - startTime ) + " ms." );

            // check reportid exist, if exist throw an XDocReportException
            try {
                registry.checkReportId(reportId);
            } catch (XDocReportException e) {
                System.out.println("exist");
            }

            // Unregister report
            registry.unregisterReport(reportId);

            // Clear registry
            registry.clear();


            // close stream
            out.close();
            out2.close();
            xmlIn.close();
            in.close();


        } catch (IOException e) {
            e.printStackTrace();
        } catch (XDocReportException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }

    }
}
